package com.ae.shield.common.inventory;

import com.ae.shield.common.items.util.ItemNBTHelper;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;

import javax.annotation.Nonnull;

public class FilterInventory extends Inventory {
    private static final String TAG_FILTER = "Filter";
    private final ItemStack stack;

    public FilterInventory(ItemStack stack, int size) {
        super(size);
        this.stack = stack;

        ListNBT list = ItemNBTHelper.getList(stack, TAG_FILTER, 10, new ListNBT());
        for (int i = 0; i < size ; i++) {
            setInventorySlotContents(i, ItemStack.read(list.getCompound(i)));
        }
    }

    @Override
    public boolean isUsableByPlayer(@Nonnull PlayerEntity player) {
        return !stack.isEmpty();
    }

    @Override
    public void markDirty() {
        super.markDirty();
        ListNBT list = new ListNBT();
        for (int i = 0; i < getSizeInventory(); i++) {
            list.add(getStackInSlot(i).write(new CompoundNBT()));
        }
        ItemNBTHelper.setList(stack, TAG_FILTER, list);
    }

    //TODO 增加黑白名单
    public boolean isValid(ItemStack stack) {
        for (int i = 0; i < getSizeInventory(); i++) {
            ItemStack stackInSlot = getStackInSlot(i);
            if (stack.getItem().equals(stackInSlot.getItem())) {
                return true;
            }
        }
        return false;
    }
}
